﻿package com._public._utils._mailer
{
	public class MailerSina extends Mailer
	{
		public function MailerSina()
		{
			super();
		}
		
		
		/**
		 * 创建邮件发送步骤数据
		 * 
		 * @return Array
		 * 
		 */		
		override protected function createSendMailSteps(mail:Mail):Array {
			var steps:Array = [];
			
			steps.push("HELO laan");
			steps.push("AUTH login");
			
			steps.push(Base64.encode(userName));
			
			steps.push(Base64.encode(password));
  			
  			steps.push("MAIL FROM:<" + mail.from + ">");
  			steps.push("RCPT TO:<" + mail.tu + ">");
  			
  			if (mail.cc) {
  				var cc:Array = mail.cc;
  				
  				for(var j:uint = 0; j < cc.length; j++) {
		  			steps.push("RCPT TO:<" + cc[j] + ">");
  				}
  			}
  			
  			steps.push("DATA");
  			
			var mailData:Array = [];
			if (!mail.fromName) {
	  			mailData.push("From:" + mail.from);
			} else {
	  			mailData.push("From:" + mail.fromName);
			}
			
			if (!mail.toName) {
	  			mailData.push("To:" + mail.tu);
			} else {
	  			mailData.push("To:" + mail.toName);
			}
			
  			if (mail.subject) {
  				mailData.push("Subject:" + mail.subject);
  			}
  			
  			if (mail.cc) {
  				mailData.push("CC:" + mail.cc);
  			}
  			
  			if (mail.sender) {
  				mailData.push("Sender:" + mail.sender);
  			}
  			
  			
  			var boundary:String = "laan" + uint(Math.random() * 100000000);
  			
  			mailData.push("MIME-Version:1.0");
			//mailData.push("Content-Type:multipart/related;boundary=QQQQQQQ");
			//mailData.push("Content-Type:multipart/alternative;boundary=QQQQQQQ");
			mailData.push("Content-Type:multipart/mixed;boundary=" + boundary);
			//mailData.push("Content-Transfer-Encoding:7bit");
			
  			mailData.push("");
  			//mailData.push("sdsssss");
  			
  			
			//邮件内容
  			mailData.push("--" + boundary);
			mailData.push("Content-Type:text/html;charset=GBK");
			//mailData.push("Content-Type:text/html;boundary=QQQQQQQ");
			mailData.push("Content-Transfer-Encoding:8bit");
  			mailData.push("");
  			mailData.push(mail.body);
  			
  			/* mailData.push("--QQQQQQQ");
			mailData.push("Content-Type:text/plain;name=aa.txt");
			//mailData.push("Content-Transfer-Encoding:7bit");
  			mailData.push("");
  			mailData.push(mail.body); */
  			
  			//mailData.push("--QQQQQQQ--");
			//mailData.push("Content-Type:multipart/mixed;boundary=QQQQQQQ");
			
  			var attachments:Array = mail.attachments;
			if (attachments && attachments.length > 0) {
				
				//mailData.push("Content-Type:multipart/mixed");
				
	  			for (var i:uint = 0; i < attachments.length; i++) {
	  				var attach:Attachment = attachments[i];
	  				
	  				//if (i != 0) 
	  				mailData.push("--" + boundary);
	  				mailData.push("Content-Type:application/octet-stream;name=" + attach.name);
					mailData.push("Content-Transfer-Encoding:base64");
					
					//判断附件是否是作为内嵌资源发送
					if (attach.isInline()) {
						mailData.push("Content-Disposition:inline");
						mailData.push("Content-ID:<" + attach.contentId +">");
					} else {
						mailData.push("Content-Disposition:attachment");
					}
					
					mailData.push("");
					mailData.push(attach.data);
		  			
	  			}
  				//mailData.push("--" + boundary + "--");
  			}
  			
  			mailData.push("--" + boundary + "--");
  			
  			mailData.push("");
  			mailData.push(".");
  			//mailData.push("");
  			
  			steps.push(mailData);
  			
  			steps.push("QUIT");
  			
  			return steps;
		}
		
	}
}